/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    typedef pair<TreeNode*, unsigned int> PTI;
    int widthOfBinaryTree(TreeNode* root)
    {
        queue<PTI> q;
        unsigned int cnt = 0;
        q.push({ root, cnt });
        int ret = 0;
        while (q.size())
        {
            int sz = q.size();
            int begin = 0, end = 0;
            for (int i = 0; i < sz; i++)
            {
                PTI front = q.front();
                q.pop();
                if (front.first->left) q.push({ front.first->left, front.second * 2 + 1 });
                cnt++;
                if (front.first->right) q.push({ front.first->right, front.second * 2 + 2 });
                if (i == 0) begin = front.second;
                if (i == sz - 1) end = front.second;
            }
            ret = max(ret, end - begin + 1);
        }
        return ret;
    }
};
class Solution {
public:
    int lastStoneWeight(vector<int>& stones)
    {
        priority_queue<int> pq;
        for (auto e : stones) pq.push(e);

        while (pq.size() > 1)
        {
            int x = pq.top();
            pq.pop();
            int y = pq.top();
            pq.pop();
            pq.push(x > y ? x - y : y - x);
        }
        return pq.top();
    }
};